package com.ae.shield.common.jei;

import com.ae.shield.ModLib;
import com.ae.shield.client.gui.AssemblyScreen;
import com.ae.shield.common.gui.container.AssemblyContainer;
import com.ae.shield.common.items.ItemList;
import com.ae.shield.common.recipe.ModRecipesType;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.registration.*;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.RecipeManager;
import net.minecraft.util.ResourceLocation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

@JeiPlugin
public class JEIPlugin implements IModPlugin {
    private static final ResourceLocation ID = new ResourceLocation(ModLib.MOD_ID, "jei_plugin");
    private final ArrayList<ItemStack> hiddenItems;

    public JEIPlugin() {
        hiddenItems = JEIHider.getList();
    }

    @Override
    public ResourceLocation getPluginUid() {
        return ID;
    }

    @Override
    public void registerRecipes(IRecipeRegistration registration) {
        RecipeManager manager = Minecraft.getInstance().world.getRecipeManager();
        registration.addRecipes(getRecipes(manager, ModRecipesType.ASSEMBLY_TYPE), AssemblyCategory.ID);
    }

    @Override
    public void registerCategories(IRecipeCategoryRegistration registration) {
        IGuiHelper helper = registration.getJeiHelpers().getGuiHelper();
        registration.addRecipeCategories(new AssemblyCategory(helper));
    }

    private static Collection<?> getRecipes(RecipeManager manager, IRecipeType<?> type) {
        return manager.getRecipes().parallelStream().filter(iRecipe -> iRecipe.getType() == type).collect(Collectors.toList());
    }

//    对着相应物品按U直接跳转可合成内容
    @Override
    public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
        registration.addRecipeCatalyst(new ItemStack(ItemList.ASSEMBLY_BLOCK.get()), AssemblyCategory.ID);
    }

//    添加物品转移按钮
    @Override
    public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
        registration.addRecipeTransferHandler(AssemblyContainer.class, AssemblyCategory.ID, 36, 13, 0, 35);
    }

//    GUI内添加查看配方按钮
    @Override
    public void registerGuiHandlers(IGuiHandlerRegistration registration) {
        registration.addRecipeClickArea(AssemblyScreen.class, 64, 75, 34, 4, AssemblyCategory.ID);
    }

    @Override
    public void registerItemSubtypes(ISubtypeRegistration registration) {
        registration.useNbtForSubtypes(ItemList.EFFECT_STORAGE_ITEM.get(),
                ItemList.INTENSIFIED_SUFFIX_KID.get(),
                ItemList.SUFFIX_KID.get());
    }

    @Override
    public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
        if (!hiddenItems.isEmpty()) {
            jeiRuntime.getIngredientManager().removeIngredientsAtRuntime(VanillaTypes.ITEM, hiddenItems);
        }
    }
}
